package com.dmall.play.interview.chapter07;

import java.util.ArrayList;
import java.util.List;

public class Leetcode257V1 {

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }

        @Override
        public String toString() {
            return "TreeNode {" +
                    "val=" + val +
                    '}';
        }
    }

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        if (root == null) {
            return res;
        }

        if (root.left == null && root.right == null) {
            res.add(String.valueOf(root.val));
            return res;
        }

        List<String> leftRes = binaryTreePaths(root.left);
        List<String> rightRes = binaryTreePaths(root.right);
        for (String pre : leftRes) {
            res.add(root.val + "->" + pre);
        }
        for (String pre : rightRes) {
            res.add(root.val + "->" + pre);
        }
        return res;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.left.right = new TreeNode(5);
        root.right = new TreeNode(3);

        Leetcode257V1 leetcode257 = new Leetcode257V1();
        System.out.println(leetcode257.binaryTreePaths(root));
    }
}
